﻿using System;
using States = Eflong.MonitoringLib.MonitoringUnit.MonitoringUnitState.States;

namespace Eflong.Monitor
{
    /// <summary>
    /// 传感器故障信息
    /// </summary>
    public class SensorFaultInfo : SensorInfo
    {
        /*// 矿的ID
        public Int32 CoalMineId;

        // 矿名称

        public string CoalMineName;

        // 传感器的 ID
        public string MonUnitId;

        // 名称及地点信息

        public string Name;

        // 传感器分类

        public Int32 Modality;

        // 传感器类型

        public Int32 nType;

        // 传感器类型名
        public string sType;

        // 传感器单位

        public string sUnit;*/

        // 故障次数
        public Int32 nFaultCount;

        // 故障时长
        public float fFaultTime;

        public const int Fault_Delay_time = 1;// 30;

        /// <summary>
        /// 是故障
        /// </summary>
        //public bool IsFault;

        public SensorFaultInfo( )
        {
            CoalMineId = 0;
            nFaultCount = 0;
            fFaultTime = 0;
            IsFault = false;
            IsFinish = false;
        }

        /// <summary>
        /// 初始化故障信息
        /// </summary>
        public void SetupFaultInfo( ExcessRecord er )
        {
            Init( er );
            if ( !IsFault && er.State >= States.UnknownState )
            {
                CoalMineId = er.CoalMineId;
                MonUnitId = er.MonUnitId;
                Name = er.MonUnitName;
                sType = er.MonUnitType;

                nFaultCount = 1;
                tmStart = er.Time;

                IsFault = true;
                if ( er.Time >= DateTime.Today )
                {
                    // 今天的故障报警,需要记录

                    IsNeedRecord = true;
                }
            }
            State = er.State;
        }

        ///
        ///  更新故障信息
        ///
        public void UpdateFaultInfo( ExcessRecord er )
        {
            if ( IsFault && er.State < States.UnknownState )
            {
                IsFault = false;
                tmStop = er.Time;

                //double dt = tmStop.Subtract(tmStart).TotalSeconds;
                //fFaultTime = (float)dt / 60;
                fFaultTime = ( float )tmStop.Subtract( tmStart ).TotalMinutes;
                IsFinish = true;
            }
            tmUpdate = er.Time;
            UpdateInfo( er );
            State = er.State;
        }

        public SensorFaultInfo Clone( )
        {
            return this.MemberwiseClone( ) as SensorFaultInfo;
        }
    }
}